// 请求封装
const request = (url, params) => {
  // params {page: 1, count: 2} => "?page=1&count=2"
  if (params) {
    const arr = Object.keys(params); // ["page", "count"]
    const arr2 = arr.map((item) => item + "=" + params[item]); // ["page=1", "count=2"]
    const str = arr2.join("&"); // "page=1&count=2"
    url += "?" + str;
  }

  return fetch("http://118.24.239.63:3001/api" + url)
    .then((response) => response.json())
    .then((res) => {
      if (res.success) {
        return res;
      } else {
        showFailToast(res.msg);
      }
    });
};

export default request;
